const app = getApp()
const { descApi, pdfApi } = require('../../../api/services/yhList/index')

Page({
  data: {
    listData: [],
    params: { comId: '', divideId: '' },
    userType: '', //用户类型
    YHZG_Edit: null,
    YHPC_Edit: null,
  },

  //  获取排查单
  async getDescData() {
    const res = await descApi(this.data.params)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    this.setData({
      listData: res.rows,
    })
  },

  //企业跳转详情
  handleDesc(event) {
    let { item } = event.currentTarget.dataset
    let id = item.id

    if (this.data.YHPC_Edit) {
      // 操作排查项
      wx.navigateTo({ url: '/pages/company/checkDesc/checkDesc?id=' + id })
    } else {
      // 只读
      wx.navigateTo({ url: '/pages/company/dangerList/dangerList?id=' + id })
    }
  },

  //打印排查报告
  async handlePrint(event) {
    let { id } = event.currentTarget.dataset
    let params = { id: id }
    const res = await pdfApi(params)

    let fileName = res.msg
    let host = 'https://dy.microzl.com/pdf/' + fileName
    console.log(host)

    wx.downloadFile({
      url: host, //要预览的PDF的地址
      success: function (res) {
        if (res.statusCode === 200) {
          //成功
          var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
          wx.openDocument({
            filePath: Path, //要打开的文件路径
            success: function (res) {
              console.log('打开PDF成功')
            },
            fail: function (res) {
              console.log('fail')
            },
          })
        }
      },
      fail: function (res) {
        console.log(res) //失败
      },
    })
  },

  onLoad: function (options) {
    console.log(options)

    app.globalData.comId = options.comId
    app.globalData.comName = options.comName

    this.setData({ params: options })

    // 0是企业用户
    // 1是第三方用户（排查人员）
    // 2是管理员（排查人员

    let userInfo = wx.getStorageSync('userInfo')
    let userType = userInfo.userType

    this.setData({ userType: userType })
  },

  onShow: function () {
    this.setData({ YHZG_Edit: wx.getStorageSync('YHZG_Edit'), YHPC_Edit: wx.getStorageSync('YHPC_Edit') })
    this.getDescData()
  },
})
